package com.yaozheng.vocationaltraining.adapter;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.ImageUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.CourseCatalogItemView;
import com.yaozheng.vocationaltraining.view.CourseCatalogItemView_;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


/**
 * 课程目录设备器
 *
 * @author Administrator
 */

public class CourseCatalogAdapter extends BaseAdapter {
    private BaseActivity baseActivity;
    JSONArray dataList;
    BitmapLoader bitmapLoader;

    public CourseCatalogAdapter(BaseActivity baseActivity, JSONArray dataList) {
        super();
        this.baseActivity = baseActivity;
        this.dataList = dataList;

        bitmapLoader = new BitmapLoader(Glide.with(baseActivity),
                R.drawable.default_load_image);
    }

    public JSONArray getDataList() {
        return dataList;
    }

    @Override
    public int getCount() {
        return TypeUtils.getJsonArraySize(dataList);
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    /**
     * 得到最后id
     *
     * @return
     */
    public int getLastId() {
        if (dataList != null && TypeUtils.getJsonArraySize(dataList) > 0) {

            JSONObject itemData = TypeUtils.getJsonObject(dataList,
                    TypeUtils.getJsonArraySize(dataList) - 1);
            if (itemData != null) {
                return TypeUtils.getJsonInteger(itemData, "id", -1);
            }
        }
        return -1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CourseCatalogItemView courseCatalogItemView;
        if (convertView == null) {
            courseCatalogItemView = CourseCatalogItemView_.build(baseActivity);
        } else {
            courseCatalogItemView = (CourseCatalogItemView) convertView;
        }
        courseCatalogItemView.loadData(position,
                TypeUtils.getJsonObject(dataList, position), bitmapLoader);
        return courseCatalogItemView;
    }
}
